package com.hbzhit.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbzhit.common.utils.Constant;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.Query;
import com.hbzhit.config.CacheConfig;
import com.hbzhit.modules.sys.dao.SysDictValDao;
import com.hbzhit.modules.sys.entity.SysDictValEntity;
import com.hbzhit.modules.sys.service.SysDictValService;
import com.hbzhit.modules.sys.vo.SysDictValQueryVO;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 数据字典内容
 *
 * @author DING
 **/
@Service("sysDictValService")
public class SysDictValServiceImpl extends ServiceImpl<SysDictValDao, SysDictValEntity> implements SysDictValService {

    @Override
    public PageUtils<SysDictValEntity> queryPage(SysDictValQueryVO params) {
        IPage<SysDictValEntity> page = this.page(new Query<SysDictValEntity>().getPage(params), params.queryWrapper());
        return new PageUtils<>(page);
    }

    @Override
    public List<SysDictValEntity> queryList(SysDictValQueryVO params) {
        return this.list(params.queryWrapper());
    }

    @Override
    public List<SysDictValEntity> listAll() {
        return this.list(new LambdaQueryWrapper<SysDictValEntity>()
                .eq(SysDictValEntity::getIsDel, Constant.INT_N)
                .or()
                .eq(SysDictValEntity::getIsDel, Constant.INT_Y)
        );
    }

    @Override
    public boolean save(SysDictValEntity entity) {
        boolean ret = super.save(entity);

        //更新缓存
        CacheConfig.dictValCache.put(entity.getId(), entity);

        return ret;
    }

    @Override
    public boolean updateById(SysDictValEntity entity) {
        boolean ret = super.updateById(entity);

        //更新缓存
        CacheConfig.dictValCache.put(entity.getId(), entity);

        return ret;
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        boolean ret = super.removeByIds(idList);

        //更新缓存
        idList.forEach(id -> CacheConfig.dictValCache.get((Long)id).setIsDel(Constant.INT_Y));

        return ret;
    }
}
